import {Component, OnInit, ViewChild} from '@angular/core';
import {STData} from '@delon/abc';
import {BaseGridComponent, BaseService, ReturnForm} from '@sb/base';
import {SysRegionTreeNodeService} from '../service/sys-region-tree-node.service';
import {NzMessageService} from 'ng-zorro-antd';
import {RegionLevel, SysRegion} from '../entity/sys-region';
import {SysRegionSearchForm} from '../entity/sys-region-search-form';
import {Observable} from 'rxjs';
import {SFSchema} from '@delon/form';
import {SysCodeSelectAlainSchema} from '../../sys-code/sys-code-select-alain/sys-code-select-alain.component';

@Component({
    selector: 'sys-region',
    templateUrl: './sys-region.component.html',
    styleUrls: ['./sys-region.component.less'],
    providers: [
        {
            provide: BaseService,
            useClass: SysRegionTreeNodeService
        }
    ]
})
export class SysRegionComponent implements OnInit {

    baseParam: SysRegion = {
        parentCode: '100000',
        level: 'PROVINCE'
    };

    @ViewChild(BaseGridComponent, {static: true})
    baseGridComponent: BaseGridComponent;

    parents: {
        pname: string;
        pcode: string;
        level: RegionLevel;
    }[] = [{
        pname: '中国',
        pcode: '100000',
        level: 'PROVINCE'
    }];

    schema: SFSchema;

    constructor(
        private sysRegionService: SysRegionTreeNodeService,
        private message: NzMessageService) {
    }

    ngOnInit(): void {
        this.updateSchema(null);
    }

    itemClick(stData: STData) {
        const region = stData as SysRegion;
        if (region) {
            const level = this.sysRegionService.getNextRegionLevel(region.level);
            if (level != null) {
                this.baseParam.level = level;
                this.baseParam.parentCode = region.code;
                this.parents.push({
                    pname: region.areaName,
                    pcode: region.code,
                    level: region.level
                });
                this.baseGridComponent.reload();
            }
        } else {
            this.baseParam.parentCode = '100000';
            this.baseParam.level = 'PROVINCE';
            this.baseGridComponent.reload();
        }
    }

    regionBack(index) {
        this.parents = this.parents.slice(0, index + 1);
        if (this.parents.length > 0) {
            const a = this.parents[this.parents.length - 1];
            this.baseParam.parentCode = a.pcode;
            this.baseParam.level = a.level;
        } else {
            this.baseParam.parentCode = '100000';
            this.baseParam.level = 'PROVINCE';
        }

        this.baseGridComponent.reload();
    }

    /**
     * 自定义加载方法
     * @param page 查询对象
     * @param sysRegionService 查询service
     */
    customLoadMethod(page: SysRegionSearchForm, sysRegionService?: SysRegionTreeNodeService): Observable<ReturnForm<SysRegion[]>> {
        return sysRegionService.getChildrenNoSpatial(page.parentCode || '100000', 1);
    }

    recordWrapper(region: SysRegion) {
        // region.level = this.sysRegionService.getNextRegionLevel(region.level);
        this.updateSchema(region);
        return region;
    }

    updateSchema(region: SysRegion) {
        let len = 2;
        if (region) {
            if (region.level === 'PROVINCE') {
                len = 2;
            } else if (region.level === 'CITY') {
                len = 4;
            } else if (region.level === 'DISTRICT') {
                len = 6;
            } else if (region.level === 'STREET') {
                len = 9;
            } else if (region.level === 'CUN') {
                len = 12;
            } else if (region.level === 'GROUP') {
                len = 14;
            } else {

            }

            if (!region.code) {
                if (region.parentCode === '100000') {
                    region.code = '00';
                } else {
                    if (region.level === 'STREET' || region.level === 'CUN') {
                        region.code = region.parentCode + '000';
                    } else {
                        region.code = region.parentCode + '00';
                    }

                }
            }

        }

        this.schema = {
            properties: {
                parentCode: {
                    type: 'string',
                    title: '父节点编码',
                    readOnly: true
                },
                level: {
                    type: 'string',
                    title: '当前节点级别',
                    ui: {
                        widget: 'sys-code-select-alain2',
                        category: '_REGION_'
                    } as SysCodeSelectAlainSchema,
                    readOnly: true
                },
                code: {
                    type: 'string',
                    title: '编码',
                    minLength: len,
                    maxLength: len
                },
                areaName: {
                    type: 'string',
                    title: '区划名称',
                },
                shortName: {
                    type: 'string',
                    title: '简称',
                    ui: {
                        hidden: true
                    }
                }
            },
            required: ['areaName', 'code', 'parentCode', 'level']
        };
    }

    importNextLevel() {
        const parentCode = this.baseParam.parentCode;
        this.sysRegionService.importNextLevel(parentCode).subscribe(
            v => {
                if (v.success) {
                    this.message.info('导入成功!');
                    this.baseGridComponent.reload();
                } else {
                    this.message.info('导入失败!');
                }
            }
        );
    }
}
